import 'dart:io';

// Constants for temperature limits
const int minTemperature = 15;
const int maxTemperature = 30;

// Device status variables
bool lightsOn = false;
int thermostatTemperature = 20;
bool securitySystemArmed = false;

void main() {
  print("Welcome to the Smart Home System!");

  while (true) {
    // Display menu
    print("\nPlease choose an action:");
    print("1. Toggle lights");
    print("2. Set thermostat temperature");
    print("3. Arm/disarm security system");
    print("4. View system status");
    print("5. Exit");

    // Get user input
    stdout.write("Enter your choice: ");
    String? choice = stdin.readLineSync();

    // Process user choice
    switch (choice) {
      case '1':
        toggleLights();
        break;
      case '2':
        setThermostat();
        break;
      case '3':
        toggleSecuritySystem();
        break;
      case '4':
        viewSystemStatus();
        break;
      case '5':
        print("Exiting the system. Goodbye!");
        return;
      default:
        print("Invalid choice. Please try again.");
    }
  }
}

// Function to toggle lights
void toggleLights() {
  lightsOn = !lightsOn;
  print("Lights are now ${lightsOn ? 'ON' : 'OFF'}.");
}

// Function to set thermostat temperature
void setThermostat() {
  while (true) {
    stdout.write("Set thermostat temperature ($minTemperature°C - $maxTemperature°C): ");
    String? input = stdin.readLineSync();

    if (input != null) {
      int? temperature = int.tryParse(input);

      if (temperature != null && temperature >= minTemperature && temperature <= maxTemperature) {
        thermostatTemperature = temperature;
        print("Thermostat set to ${thermostatTemperature}°C.");
        break;
      } else {
        print("Invalid temperature. Please enter a value between $minTemperature and $maxTemperature.");
      }
    }
  }
}

// Function to arm/disarm security system
void toggleSecuritySystem() {
  securitySystemArmed = !securitySystemArmed;
  print("Security system is now ${securitySystemArmed ? 'ARMED' : 'DISARMED'}.");
}

// Function to view system status
void viewSystemStatus() {
  print("\nSystem Status:");
  print("- Lights: ${lightsOn ? 'ON' : 'OFF'}");
  print("- Thermostat: ${thermostatTemperature}°C - ${getTemperatureStatus()}");
  print("- Security System: ${securitySystemArmed ? 'ARMED' : 'DISARMED'}");
}

// Function to get temperature status (FizzBuzz-like feature)
String getTemperatureStatus() {
  if (thermostatTemperature % 3 == 0 && thermostatTemperature % 5 == 0) {
    return "Perfect Day";
  } else if (thermostatTemperature % 3 == 0) {
    return "Cool Day";
  } else if (thermostatTemperature % 5 == 0) {
    return "Warm Day";
  } else {
    return "";
  }
}